from threading import Semaphore,Thread
from time import sleep


def room(name,se):
    se.acquire()  # 获取信号量
    print(f'{name}进入教室')
    sleep(2)  # 模拟在教室内的操作
    print(f'{name}离开教室')
    se.release()  # 释放信号量


if __name__ == '__main__':
    se = Semaphore(3)
    for i in range(10):
        t = Thread(target=room,args=(f'同学{i+1}',se))
        t.start()
        # t.join()  # 等待线程结束，确保每个线程都能执行完